package com.example.portdemo.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.portdemo.R;
import com.example.portdemo.date.model.NewsEntity;

import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {

    private List<NewsEntity> newsList;

    // 构造函数
    public NewsAdapter(List<NewsEntity> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.news_item, parent, false);
        return new NewsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        NewsEntity newsItem = newsList.get(position);
        holder.titleTextView.setText(newsItem.getTitle());
        holder.contentTextView.setText(newsItem.getContent());
        // 可以设置其他视图元素，例如图片、日期等
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    // ViewHolder 类
    public static class NewsViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;
        public TextView contentTextView;

        public NewsViewHolder(View view) {
            super(view);
            titleTextView = view.findViewById(R.id.titleTextView);
            contentTextView = view.findViewById(R.id.contentTextView);
            // 初始化其他视图元素
        }
    }

    // 更新新闻列表的方法
    public void setNewsList(List<NewsEntity> newsList) {
        this.newsList = newsList;
        notifyDataSetChanged();
    }
}